package cn.lsngo.manager;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    private static final int UPDATE_TIME = 0x001;
    private onReceiveTime mOnReceiveTime;
    private static String LOCAL_DATE = "yyyy-MM-dd";

    public DateUtil() {
    }

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_TIME:
                    if (mOnReceiveTime != null) {
                        mOnReceiveTime.onReceiveCurrentTime(long2Date(System.currentTimeMillis()));
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public static String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = simpleDateFormat.format(new Date());
        return currentTime;
    }

    public static String getCurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = simpleDateFormat.format(new Date());
        return currentTime;
    }

    public static boolean isData(String content) {
        if (isDateString(content, "yyyy.MM.dd")) {
            return true;
        }
        return false;
    }

    public static boolean isDateString(String dateValue, String dateFormat) {
        if (TextUtils.isBlank(dateValue) || dateValue == null) {
            return false;
        }
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            Date dd = fmt.parse(dateValue);
            if (dateValue.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static int compareTime(String oldTime) throws ParseException {
        //1、指定两个Date对象，业务中可以以参数的形式传递，不用new当前时间。
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date(); //当前时间
        Date yesterdayDate = dateFormat.parse(oldTime);//假设一个时间，工作业务中可以参数传递的形式来获取。
        //2、时间戳相减
        long yesterdayDateTime = yesterdayDate.getTime();//昨天的时间戳
        long nowDateTime = nowDate.getTime();//当前时间戳
        int result = (int) (nowDateTime - yesterdayDateTime);//毫秒
        //3、换算成秒
        int diffSecond = result / 1000; //1000毫秒等于1秒
        //4、换算成分钟
        int diffMinute = result / 1000 / 60;//60秒等于1分钟
        //5、换算成是小时
        int diffHour = result / 1000 / 60 / 60;//60分钟等于1小时
        return diffMinute;
    }


    public void getCurrentTime(onReceiveTime onReceiveTime) {
        this.mOnReceiveTime = onReceiveTime;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(UPDATE_TIME);
                }
            }
        }).start();
    }

    /**
     * 毫秒数转换时间
     *
     * @param time
     * @return
     */
    public static String long2Date(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date((long) time);
        return simpleDateFormat.format(date);
    }

    /**
     * 毫秒数转换日期
     *
     * @param date
     * @return
     */
    public static String date2DateStr(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(LOCAL_DATE, Locale.CHINA);
        String format = simpleDateFormat.format(date);
        return format;
    }


    /**
     * 毫秒数转换时间
     *
     * @param time
     * @return
     */
    public static String long2Date(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        long time2long = 0;
        try {
            time2long = Long.parseLong(time);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            time2long = 0;
        }
        Date date = new Date(time2long);
        return simpleDateFormat.format(date);
    }

    /**
     * 毫秒数转换时间
     *
     * @param time
     * @return
     */
    public static String long2Date(long time, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        Date date = new Date((long) time);
        return simpleDateFormat.format(date);
    }

    /**
     * 毫秒数转换时间
     *
     * @param time
     * @return
     */
    public static Date str2Date(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date(System.currentTimeMillis());
        }
        return date;
    }


    public interface onReceiveTime {
        void onReceiveCurrentTime(String time);
    }
}
